// pages/resultype/lacrimalduct/lacrimalduct.js
const commonutil = require("../commonutil.js");
const screenresultservice = require("../../../apiservice/screen/screenresultservice.js");
const jsonData = require("../jsondata.js");
const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		scid: '',
		customerid: '',
		leadName: '',
		addOrMod:'add',
		/**选项集**/
		tearPointSizeIndex: -1,
		lacrimalAtresiaIndex: -1,
		lacrimalSacSecretionIndex: -1,
		eyeDroppingsIndex: -1,
		shedTearsIndex: -1,
		lacrimalDuctObstructionIndex: -1,
		haveOrNot:jsonData.haveOrNot,
		notOrRareOrOften:jsonData.notOrRareOrOften,
		noneOrBothOrLeftOrRight:jsonData.noneOrBothOrLeftOrRight,
		lacrimalAtresiaArray:jsonData.lacrimalAtresiaArray,
		normalOrAbnormal:jsonData.normalOrAbnormal,
    schoolId:'',
		/**泪道检查**/
		//泪点大小
		tearPointSize: {
			new_scid: null,
			new_categoryid: 'LB0003',
			new_itemid: 'XM0018',
			new_contentid: 'NR0051',
			new_result: 'SC0000000013',
			new_doctorid: null,
			ownerid: null
		},
		//泪点大小具体描述
		tearPointDescription: {
			new_scid: null,
			new_categoryid: 'LB0003',
			new_itemid: 'XM0018',
			new_contentid: 'NR0053',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//泪点闭锁
		lacrimalAtresia: {
			new_scid: null,
			new_categoryid: 'LB0003',
			new_itemid: 'XM0015',
			new_contentid: 'NR0054',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//挤压泪囊有无分泌物
		lacrimalSacSecretion: {
			new_scid: null,
			new_categoryid: 'LB0003',
			new_itemid: 'XM0016',
			new_contentid: 'NR0056',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//泪道病史-眼角眼屎
		eyeDroppings: {
			new_scid: null,
			new_categoryid: 'LB0003',
			new_itemid: 'XM0021',
			new_contentid: 'NR0058',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//泪道病史-流泪
		shedTears: {
			new_scid: null,
			new_categoryid: 'LB0003',
			new_itemid: 'XM0020',
			new_contentid: 'NR0061',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//初步诊断-泪道阻塞
		lacrimalDuctObstruction: {
			new_scid: null,
			new_categoryid: 'LB0003',
			new_itemid: 'XM0004',
			new_contentid: 'NR0064',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var options = options;
		if (options) {
			this.setData({
				leadName: options.leadname,
				customerid: options.sccustid,
				scid: options.scid,
				categoryid: options.categoryid
			})
			this.getLacrimalductResults(options.scid, options.sccustid, options.categoryid);
      if(options.schoolId){
        this.setData({schoolId:options.schoolId});
      }
		}
	},
	//提交表单
	lacrimalductSubmit: function(e) {
		var createResult = this.getLacrimalduct();
		var that = this;
		commonutil.createScreenResults(createResult,function(res){
			var idArray = res.result.split('|');
			for(var i = 0;i < idArray.length;i++){
				createResult[i].new_sc_resultId = idArray[i];
			}
			that.setData({
				addOrMod: "mod",
				tearPointSize:	createResult[0],
				tearPointDescription:	createResult[1],
				lacrimalAtresia:	createResult[2],
				lacrimalSacSecretion:	createResult[3],
				eyeDroppings:	createResult[4],
				shedTears:	createResult[5],
				lacrimalDuctObstruction:	createResult[6],
			});
		});
	},
	//更新
	lacrimalductUpdate:function(){
		var updateResult = this.getLacrimalduct();
		commonutil.updateScreenResults(updateResult);
	},
	//查看报告
	enterDetail: async function(e) {
		var scid = this.data.scid;
		var leadid = this.data.customerid;
		var lead = {};
		var data = await commonutil.getLeadInfo(leadid);
		lead.leadid = data.leadId;
		lead.customername = data.lastName;
		lead.age = data.new_age;
		lead.sex = data.new_gender;
		lead.iphoneno1 = data.new_phone;
		lead.sfz = data.new_citizenid;
		lead.createdon = data.modifiedOn;
    var url = "/pages/print/letter/letter?scid=" + scid;
    if (this.data.schoolId)
      url += "&schoolId=" + this.data.schoolId;
		wx.navigateTo({
			url: url,
			events: {
				// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
				acceptDataFromOpenedPage: function(data) {
					console.log(data)
				},
				someEvent: function(data) {
					console.log(data)
				}
			},
			success: function(res) {
				// 通过eventChannel向被打开页面传送数据
				res.eventChannel.emit('acceptDataFromOpenerPage', {
					data: lead
				})
			}
		})
	},
	//获取结果集
	getLacrimalductResults: async function(scid, customerid, categoryid) {
		wx.showLoading({
			title:'加载中...',
			mask:true,
		})
		var success = await screenresultservice.GetScreenResultByCategory(scid, customerid, categoryid);
		var reusltArray = success.data
		if(reusltArray.length==0){
			wx.hideLoading();
			return ;
		}
		this.setData({
			addOrMod:'mod'
		})
		for (let index in reusltArray) {
			var element = reusltArray[index];
			if (element.new_contentid === 'NR0051') {
				this.setData({
					tearPointSize: element
				})
			} else if (element.new_contentid === 'NR0053') {
				this.setData({
					tearPointDescription: element
				})
			} else if (element.new_contentid === 'NR0054') {
				this.setData({
					lacrimalAtresia: element
				})
			} else if (element.new_contentid === 'NR0056') {
				this.setData({
					lacrimalSacSecretion: element
				})
			} else if (element.new_contentid === 'NR0058') {
				this.setData({
					eyeDroppings: element
				})
			}else if (element.new_contentid === 'NR0061') {
				this.setData({
					shedTears: element
				})
			} else if (element.new_contentid === 'NR0064') {
				this.setData({
					lacrimalDuctObstruction: element
				})
			}
		}
		wx.hideLoading();
	},
	/**选项集、文本赋值**/
	onTearPointSizeChange: function(e) {
		var result = 'tearPointSize.new_result';
		this.setData({
			[result]:'',
			tearPointSizeIndex: e.detail.index
		})
	},
	onTearPointDescriptionChange: function(e) {
		var result = 'tearPointDescription.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onLacrimalAtresiaChange: function(e) {
		this.setData({
			lacrimalAtresiaIndex: e.detail.index
		})
	},
	onLacrimalSacSecretionChange: function(e) {
		this.setData({
			lacrimalSacSecretionIndex: e.detail.index
		})
	},
	onEyeDroppingsChange: function(e) {
		this.setData({
			eyeDroppingsIndex: e.detail.index
		})
	},
	onShedTearsChange: function(e) {
		this.setData({
			shedTearsIndex: e.detail.index
		})
	},
	onLacrimalDuctObstructionChange: function(e) {
		this.setData({
			lacrimalDuctObstructionIndex: e.detail.index
		})
	},
	/**获取createResult**/
	getLacrimalduct: function() {
		var createResult = [];
		var scid = this.data.scid;
		var customerid = this.data.customerid;
		var tearPointSize = this.data.tearPointSize;
		var tearPointDescription = this.data.tearPointDescription;
		var lacrimalAtresia = this.data.lacrimalAtresia;
		var lacrimalSacSecretion = this.data.lacrimalSacSecretion;
		var eyeDroppings = this.data.eyeDroppings;
		var shedTears = this.data.shedTears;
		var lacrimalDuctObstruction = this.data.lacrimalDuctObstruction;
		//泪点大小
		var tearPointSizeIndex = this.data.tearPointSizeIndex;
		var normalOrAbnormal = this.data.normalOrAbnormal;
		if (tearPointSizeIndex >-1) {
			tearPointSize.new_result = normalOrAbnormal[tearPointSizeIndex];
		} 
		//泪点闭锁
		var lacrimalAtresiaIndex = this.data.lacrimalAtresiaIndex;
		var lacrimalAtresiaArray = this.data.lacrimalAtresiaArray;
		if (lacrimalAtresiaIndex >-1) {
			lacrimalAtresia.new_result = lacrimalAtresiaArray[lacrimalAtresiaIndex];
		}
		//挤压泪囊有无分泌物
		var lacrimalSacSecretionIndex = this.data.lacrimalSacSecretionIndex;
		var haveOrNot = this.data.haveOrNot;
		if (lacrimalSacSecretionIndex >-1) {
			lacrimalSacSecretion.new_result = haveOrNot[lacrimalSacSecretionIndex];
		}
		//眼角眼屎
		var eyeDroppingsIndex = this.data.eyeDroppingsIndex;
		var notOrRareOrOften = this.data.notOrRareOrOften;
		if (eyeDroppingsIndex >-1) {
			eyeDroppings.new_result = notOrRareOrOften[eyeDroppingsIndex];
		} 
		//流泪
		var shedTearsIndex = this.data.shedTearsIndex;
		if (shedTearsIndex >-1) {
			shedTears.new_result = notOrRareOrOften[shedTearsIndex];
		}
		//泪道阻塞
		var lacrimalDuctObstructionIndex = this.data.lacrimalDuctObstructionIndex;
		var noneOrBothOrLeftOrRight = this.data.noneOrBothOrLeftOrRight;
		if (lacrimalDuctObstructionIndex >-1) {
			lacrimalDuctObstruction.new_result = noneOrBothOrLeftOrRight[lacrimalDuctObstructionIndex];
		} 
		createResult.push(tearPointSize);
		if(tearPointSize.new_result=='SC0000000005'&&tearPointDescription.new_result!=''){
			createResult.push(tearPointDescription);
		}
		createResult.push(lacrimalAtresia);
		createResult.push(lacrimalSacSecretion);
		createResult.push(eyeDroppings);
		createResult.push(shedTears);
		createResult.push(lacrimalDuctObstruction);
		//给各结果的筛查id/联系人id赋值
		createResult.forEach(element => {
			element.new_scid = scid;
			element.new_customerid = customerid;
		});
		return createResult;
	}

})
